﻿using AutoMapper;
using BlogDemo.Application.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlogDemo.Application
{
    /// <summary>
    ///  AutoMapper帮助类
    /// </summary>
    public static class AutoMapperExtension
    {
        /// <summary>
        ///  单个对象映射
        /// </summary>
        public static TDestination MapTo<TDestination>(this object source)
        {
            if (source == null) return default(TDestination);

            return AutoMapperConfiguration.Mapper.Map<TDestination>(source);
        }
        /// <summary>
        ///  单个对象映射
        /// </summary>
        public static TDestination MapTo<TDestination>(this object source, TDestination destination)
        {
            if (source == null) return default(TDestination);

            return (TDestination)AutoMapperConfiguration.Mapper.Map(source, destination, source.GetType(), typeof(TDestination));
        }
        /// <summary>
        ///  单个对象映射
        /// </summary>
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            if (source == null) return default(TDestination);
            return AutoMapperConfiguration.Mapper.Map<TDestination>(source);
        }

        /// <summary>
        ///  单个对象映射
        /// </summary>
        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
            where TSource : class
            where TDestination : class
        {
            if (source == null) return default(TDestination);
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapTo<TSource, TDestination>(this IEnumerable<TSource> source)
        {
            var destination = new List<TDestination>();
            if (source != null)
            {
                foreach (var s in source)
                {
                    destination.Add(AutoMapperConfiguration.Mapper.Map<TSource, TDestination>(s));
                }
            }
            return destination;
         }
       
    }
}
